﻿Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports Microsoft.Win32
Imports System.IO
Imports ESBD.Search_Results

Public Class Delete_Record

    Private Sub BTN_Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Delete.Click
        'Msgbox asking user if they are sure they want to permanentely delete record
        Dim qDelete As DialogResult

        qDelete = MsgBox("Permanently delete this record from the database?", MsgBoxStyle.OkCancel Or MsgBoxStyle.Question)

        If qDelete = DialogResult.OK Then
            GoTo Ask_PW
        Else
            GoTo Finish
        End If

Ask_PW:
        'Ask for password
        If DelRec_PW <> "" Then
            If Admin_PW_Entered = "Y" Or DelRec_PW_Entered = "Y" Then
                GoTo Del_Rec
            Else
                Dim Passwd As String
                Passwd = InputBox("Please enter the password to Delete Records or the Administrator password.", "Delete Records Password")
                If Passwd = Admin_PW Then
                    Admin_PW_Entered = "Y"
                    EditRec_PW_Entered = "Y"
                    DelRec_PW_Entered = "Y"
                    GoTo Del_rec
                ElseIf Passwd = DelRec_PW Then
                    DelRec_PW_Entered = "Y"
                    GoTo Del_rec
                ElseIf Passwd = "" Then
                    GoTo Finish
                Else
                    MsgBox("Sorry the password you entered did not match the password on file." + vbCrLf + "Please check spelling and capitalization and try again.", MsgBoxStyle.Information)
                    GoTo Finish
                End If
            End If
        Else
            GoTo Del_Rec
        End If

Del_Rec:
        'Delete Record
        ESBD_Form.OpenSQLConnection()
        Dim conn As New SqlConnection(csb.ToString())
        Try
            cmd.Connection = conn
            conn.Open()

            'Delete Record
            cmd.CommandText = "Delete From ESBD_Main where Job_ID = '" + TB_JobID.Text + "'"
            cmd.ExecuteScalar()

            'Delete attached file if associated with Record
            If TB_AttachFile.Text <> "" Then
                cmd.CommandText = "Delete From Attachments_TB where Filename = '" + TB_AttachFile.Text + "'"
                cmd.ExecuteScalar()
            End If
            conn.Close()

            MsgBox("Record has been deleted from the database.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
            Se_Re.Dispose()
            Search_Form.Btn_Search.PerformClick()
            Me.Close()

        Catch ex As Exception
            MsgBox(ex.message + vbCrLf + vbCrLf + "Connection Failed")
            conn.Close()
            Me.Cursor = Cursors.Default
        End Try

Finish:

    End Sub
End Class